package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import com.changxianggu.cloud.crawler.core.utils.UserAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class RenMinDaXue {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "__root_domain_v=.crup.com.cn; _qddaz=QD.na4my2.9miq4g.k6vpwl52; Hm_lvt_27ad556c2ff2cdcaf4f5255518c49365=1582261987,1582786437,1583744224,1583816093";
    public static final String ORIGIN = "http://www.crup.com.cn";
    public static final String REFERER = "http://www.crup.com.cn/Book/List?keyword=";
    public static final String URL_PRE = "http://www.crup.com.cn";

    @Test
    public void initCrawler() throws Exception {
        doCrawler();
    }

    public void doCrawler() throws Exception {
        int pageNum =2875;
        int totalPage = 2890;
        int pageSize = 10;
        for (int i = pageNum; i <= totalPage; i++) {
            String url = "http://www.crup.com.cn/Book/BindCourseList?Keywords=&OrderByType=&IssueDate=&Classify=&Parent=&Qualifications=1.&Series=";
            Map<String, Object> parameter = MapUtil.newHashMap(4);
            parameter.put("Page", i);
            parameter.put("Size", pageSize);
            Map<String, Object> headers = MapUtil.newHashMap(5);
            headers.put("Cookie", COOKIE);
            headers.put("ORIGIN", ORIGIN);
            headers.put("User-Agent", UserAgentUtil.random());
            headers.put("Referer", REFERER);
            String result = HttpUtil.doPost(url, headers, parameter);
            if (StrUtil.isNotBlank(result)) {
                JSONObject jsonObject = JSONObject.parseObject(result);
                Boolean errcode = jsonObject.getBoolean("Success");
                if (errcode != null && errcode == true) {
                    JSONArray listObject = jsonObject.getJSONArray("model");
                    int size = listObject.size();
                    List<CxgBook> listBook = new ArrayList<>();
                    for (int k = 0; k < size; k++) {
                        JSONObject jsonTmp = listObject.getJSONObject(k);
                        CxgBook bookEntity = getBookEntity(jsonTmp.getString("ID"));
                        bookEntity.setIsSync(0);
                        bookEntity.setCreateTime(LocalDateTime.now());
                        bookEntity.setPressName("中国人民大学出版社");
                        bookEntity.setStatus(1);
                        //出版社Id不明
                        bookEntity.setPressId(79);
                        listBook.add(bookEntity);
                    }
                    bookService.saveBatch(listBook);
                    log.info("第" + i + "页数据保存成功");
                    log.warn("--->" + i);
                }
            }
        }
    }

    public CxgBook getBookEntity(String numId) throws Exception {
        CxgBook bookEntity = new CxgBook();
        String url = "http://www.crup.com.cn/Book/TextDetail?doi="+numId;
        Map<String, Object> headers = MapUtil.newHashMap(5);
        headers.put("Cookie", COOKIE);
        headers.put("User-Agent", UserAgentUtil.random());
        headers.put("Referer", REFERER);
        String result = HttpUtil.doPost(url, headers);
        while (result.contains("页面没有找到")) {
            //睡眠2秒继续访问
            Thread.sleep(2000);
            result = HttpUtil.doPost(url, headers);
        }
        if (StrUtil.isNotBlank(result)) {
            Document htmlDoc = Jsoup.parse(result);
            Element bookContent = htmlDoc.select("body").first();
            if (bookContent != null) {
                bookEntity.setPkId(numId);

                //isbn
                Element isbnElement = bookContent.select(".no-border-right li").eq(1).first();
                if (isbnElement != null) {
                    String isbnNumStr = isbnElement.text().replace("ISBN：","").replace("-","");
                    if (StrUtil.isNotBlank(isbnNumStr)) {
                        bookEntity.setBookIsbn(isbnNumStr);
                    }
                }

                //name
                Element nameElement = bookContent.select(".book-title span").first();
                if (nameElement != null) {
                    String nameStr = nameElement.text();
                    if (StrUtil.isNotBlank(nameStr)) {
                        bookEntity.setBookName(nameStr);
                    }
                }

                Element wordNumElement = bookContent.select(".book-det-list ul").eq(1).select("li").first();
                if (wordNumElement != null) {
                    String wordNumStr = wordNumElement.text();
                    if (StrUtil.isNotBlank(wordNumStr)) {
                        bookEntity.setWordNum(Integer.parseInt(getNumbers(wordNumStr)));
                    }
                }

                Element authorElement = bookContent.select(".book-det-list ul").first().select("li").first();
                if (authorElement != null) {
                    String authorStr = authorElement.text().replace("作者：","");
                    if (StrUtil.isNotBlank(authorStr)) {
                        bookEntity.setAuthor(authorStr);
                    }
                }

                Element priceElement = bookContent.select(".book-det-list ul").first().select("li").eq(2).first();
                if (priceElement != null) {
                    String priceStr = priceElement.text()
                            .replace("定价：","")
                            .trim()
                            .replace("￥","")
                            .replace("元","").trim();
                    if (StrUtil.isNotBlank(priceStr)) {
                        bookEntity.setPrice(new BigDecimal(priceStr));
                    }
                }

                Element digUriElement = bookContent.select(".read").first();
                if (digUriElement != null) {
                    String digUriStr = digUriElement.attr("href");
                    if (StrUtil.isNotBlank(digUriStr)) {
                        bookEntity.setDigitalUri(digUriStr);
                    }else {
                        bookEntity.setDigitalUri("无");
                    }
                }

                //catalog
                Element catalogElement = bookContent.select(".directory").select("p").first();
                if (catalogElement != null) {
                    String catalogStr = catalogElement.text();
                    if (StrUtil.isNotBlank(catalogStr)) {
                        bookEntity.setCatalog(catalogStr);
                    }
                }

                //brief
                Element briefElement = bookContent.select(".info-con-text").first();
                if (briefElement != null) {
                    String briefStr = briefElement.text();
                    if (StrUtil.isNotBlank(briefStr)) {
                        bookEntity.setBrief(briefStr);
                    }
                }

                //print_version
                Element pvElement = bookContent.select(".book-det-list ul").eq(1).select("li").eq(1).first();
                if (pvElement != null) {
                    String pvStr = pvElement.text().replace("印次：","");
                    if (StrUtil.isNotBlank(pvStr)) {
                        bookEntity.setPrintVersion(pvStr);
                    }
                }

                //courseware_uri
                Element cuElement = bookContent.select(".res-teaching-con a").first();
                if (cuElement != null) {
                    String cuStr = URL_PRE+cuElement.attr("href");
                    if (StrUtil.isNotBlank(cuStr)) {
                        bookEntity.setCoursewareUri(cuStr);
                    }
                }
            }
        }
        return bookEntity;
    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
